using System;
using System.Collections.Generic;
using Csla;
using System.Data;
using ENT.GP.Datos.MysqlHelper;

namespace ENT.GP.Core
{
    [Serializable]
    public class ListaCatalogos :
      ReadOnlyListBase<ListaCatalogos, ListaCatalogo>
    {
        #region Authorization Rules

        private static void AddObjectAuthorizationRules()
        {
            // TODO: add authorization rules
            //AuthorizationRules.AllowGet(typeof(ListaCatalogos), "Role");
        }

        #endregion

        #region Factory Methods

        public static ListaCatalogos GetListaCatalogos(string filter,int idEmpresa)
        {
            MultipleCriteria obj = new MultipleCriteria(filter, idEmpresa);
            return DataPortal.Fetch<ListaCatalogos>(new SingleCriteria<ListaCatalogos, MultipleCriteria>(obj));
        }

        private ListaCatalogos()
        { /* require use of factory methods */ }

        #endregion


        #region Data Access

        private void DataPortal_Fetch(SingleCriteria<ListaCatalogos, MultipleCriteria> prm)
        {
                      
            RaiseListChangedEvents = false;
            IsReadOnly = false;

            HelperMySql objhm = HelperMySql.ObtenerConexion("");
            MySql.Data.MySqlClient.MySqlParameter[] parametros = new MySql.Data.MySqlClient.MySqlParameter[2];

            parametros[0] = new MySql.Data.MySqlClient.MySqlParameter("CODIGO", prm.Value.Criterio);
            parametros[1] = new MySql.Data.MySqlClient.MySqlParameter("IDEMPRESA1", prm.Value.IdEmpresa);
            foreach (DataRow items in objhm.EjecutarStoreProcedure(parametros, "Listar_Item_X_Codigo").Tables[0].Rows)
            {
                this.Add(ListaCatalogo.GetListaCatalogo(items));
            }

            IsReadOnly = true;
            RaiseListChangedEvents = true;
        }

        #endregion
    }

    class MultipleCriteria 
    {
        public string Criterio { get; set; }
        public int IdEmpresa { get; set; }
        public MultipleCriteria(string sCrit, int idE)
        {
            Criterio  = sCrit;
            IdEmpresa = idE;
       }
    }

    [Serializable]
    public class ListaCatalogo : ReadOnlyBase<ListaCatalogo>
    {
        #region Business Methods

        public int Id { get; set; }

        public int IdCatalogoPadre { get; set; }

        public string Codigo { get; set; }

        public string Nombre { get; set; }

        public string Descripcion { get; set; }

        #endregion

        #region Factory Methods

        internal static ListaCatalogo GetListaCatalogo(DataRow childData)
        {
            return DataPortal.FetchChild<ListaCatalogo>(childData);
        }

        private ListaCatalogo()
        { /* require use of factory methods */ }

        #endregion

        #region Data Access

        private void Child_Fetch(DataRow childData)
        {
            int id, idp;
            int.TryParse(childData[0].ToString(),out id);
            Id = id;
            Codigo = childData[1].ToString();
            Nombre = childData[2].ToString();
            Descripcion = childData[3].ToString();
            int.TryParse(childData[4].ToString(), out idp);
            IdCatalogoPadre = idp;
        }

        #endregion
    }
}
